
using TMPro;
using UnityEngine;
using UnityEngine.UI;
// 基础设置
public class JichuSet : MonoBehaviour
{

    [SerializeField] private SetSelect setSelect;
    [SerializeField] private Button[] buttons;
    private int index;
    public bool set;

    private int currency = 1;
    private int gameDefaultTime = 60;
    private int idleTime = 20;
    private int backgroundValue = 100;
    private int effectsValue = 100;

    private int currencyDefault = 1;
    private int gameDefaultTimeDefault = 60;
    private int idleTimeDefault = 20;
    private int backgroundValueDefault = 100;
    private int effectsValueDefault = 100;

    [SerializeField] private TextMeshProUGUI currencyText;
    [SerializeField] private TextMeshProUGUI gameDefaultTimeText;
    [SerializeField] private TextMeshProUGUI idleTimeText;
    [SerializeField] private TextMeshProUGUI backgroundValueText;
    [SerializeField] private TextMeshProUGUI effectsValueText;

    [SerializeField] private GameObject Tooptip;

    [SerializeField] private TextMeshProUGUI title;

    [SerializeField] private TextMeshProUGUI currencyTextTiTle;
    [SerializeField] private TextMeshProUGUI gameDefaultTimeTextTiTle;
    [SerializeField] private TextMeshProUGUI idleTimeTextTiTle;
    [SerializeField] private TextMeshProUGUI backgroundValueTextTiTle;
    [SerializeField] private TextMeshProUGUI effectsValueTextTiTle;
    [SerializeField] private TextMeshProUGUI saveAndesc;
    [SerializeField] private TextMeshProUGUI exit;
    [SerializeField] private TextMeshProUGUI restoreDefault;


    void Start()
    {
        SetText();
    }

    private void OnEnable()
    {
        GetData();
    }
    // Update is called once per frame
    void Update()
    {

        buttons[index].Select();
        if (!set)
        {
            JichuSelect();
        }
        else
        {
            JichuSetValue();
        }

        JichuConfirm();

        SetText();
    }

    private void JichuConfirm()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.J) || ComTrans.Instance.GetsecSerial() || ComTrans.Instance.GetsetSerial())
        {
            if (!set)
            {
                if (index >= 5)
                {
                    Tooptip.SetActive(true);
                    set = true;
                    switch (index)
                    {
                        case 5:
                            if (setSelect.language == 0)
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否保存并退出", "SaveAndEsc");
                            }
                            else
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to save and exit", "SaveAndEsc");
                            }
                            break;
                        case 6:
                            if (setSelect.language == 0)
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否不保存退出", "DontSave");
                            }
                            else
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to exit without saving", "DontSave");
                            }
                            break;
                        case 7:
                            if (setSelect.language == 0)
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否恢复默认设置", "SaveAndDefault");
                            }
                            else
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to restore default settings", "SaveAndDefault");
                            }
                            break;
                    }
                    return;
                }
                for (int i = 0; i < 5; i++)
                {
                    if (i == index)
                    {
                        buttons[i].transform.GetChild(2).gameObject.SetActive(true);
                    }
                    else
                    {
                        buttons[i].transform.GetChild(2).gameObject.SetActive(false);
                    }
                }
                set = true;
            }
            else
            {
                set = false;
                for (int i = 0; i < 5; i++)
                {
                    buttons[i].transform.GetChild(2).gameObject.SetActive(false);
                }
            }
        }
    }

    // 选择修改
    private void JichuSelect()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            if (--index < 0) index = buttons.Length - 1;
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            if (++index >= buttons.Length) index = 0;
        }

    }

    //修改值
    private void JichuSetValue()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            if (index == 0)
            {
                currency--;
                if (currency < 0) currency = 0;
            }
            else if (index == 1)
            {
                gameDefaultTime -= 10;
                if (gameDefaultTime < 10) gameDefaultTime = 10;
            }
            else if (index == 2)
            {
                backgroundValue--;
                if (backgroundValue < 0) backgroundValue = 0;
            }
            else if (index == 3)
            {
                effectsValue--;
                if (effectsValue < 0) effectsValue = 0;
            }
            else if (index == 4)
            {
                idleTime--;
                if (idleTime < 0) idleTime = 0;
            }
            else
            {
                return;
            }
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            if (index == 0)
            {
                currency++;
                if (currency > 100) currency = 100;
            }
            else if (index == 1)
            {
                gameDefaultTime += 10;
                if (gameDefaultTime > 3600) gameDefaultTime = 3600;
            }
            else if (index == 2)
            {
                backgroundValue++;
                if (backgroundValue > 100) backgroundValue = 100;
            }
            else if (index == 3)
            {
                effectsValue++;
                if (effectsValue > 100) effectsValue = 100;
            }
            else if (index == 4)
            {
                idleTime++;
                if (idleTime > 3600) idleTime = 3600;
            }
            else { return; }
        }
    }


    #region  设置文本

    private void SetLanguageText()
    {
        if (setSelect.language == 0)
        {
            title.text = "基础设置";
            currencyTextTiTle.text = "几币一玩";
            gameDefaultTimeTextTiTle.text = "游戏时间";
            backgroundValueTextTiTle.text = "背景音量";
            effectsValueTextTiTle.text = "音效音量";
            idleTimeTextTiTle.text = "返回时间";
            saveAndesc.text = "保存并退出";
            exit.text = "不保存退出";
            restoreDefault.text = "恢复默认设置";
        }
        else if (setSelect.language == 1)
        {
            title.text = "Basic Settings";
            currencyTextTiTle.text = "Coins Play";
            gameDefaultTimeTextTiTle.text = "GAME TIME";
            backgroundValueTextTiTle.text = "Background Volume";
            effectsValueTextTiTle.text = "Sound Effect Volume";
            idleTimeTextTiTle.text = "Return Time";
            saveAndesc.text = "Save And Exit";
            exit.text = "Exit Witjout Saving";
            restoreDefault.text = "Restore Default Settings";
        }

    }

    private void SetgameDefaultTimeText()
    {
        int minutes = Mathf.FloorToInt(gameDefaultTime / 60);
        int seconds = Mathf.FloorToInt(gameDefaultTime % 60);
        string str = string.Format("{0}:{1:D2}", minutes, seconds);
        gameDefaultTimeText.text = str;
        int minutes2 = Mathf.FloorToInt(idleTime / 60);
        int seconds2 = Mathf.FloorToInt(idleTime % 60);
        string str2 = string.Format("{0}:{1:D2}", minutes2, seconds2);
        idleTimeText.text = str2;
    }

    private void SetText()
    {
        currencyText.text = currency.ToString();
        backgroundValueText.text = backgroundValue.ToString();
        effectsValueText.text = effectsValue.ToString();
        SetLanguageText();
        SetgameDefaultTimeText();
    }
    #endregion

    #region 保存

    public void SaveAndEsc()
    {
        PlayerPrefs.SetInt("currency", currency);
        PlayerPrefs.SetInt("gameDefaultTime", gameDefaultTime);
        PlayerPrefs.SetInt("idleTime", idleTime);
        PlayerPrefs.SetInt("backgroundValue", backgroundValue);
        PlayerPrefs.SetInt("effectsValue", effectsValue);
        PlayerPrefs.Save();
        ComTrans.Instance.audioSource.volume = (float)effectsValue / 100;
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);
    }

    public void SaveAndDefault()
    {
        currency = currencyDefault;
        gameDefaultTime = gameDefaultTimeDefault;
        idleTime = idleTimeDefault;
        backgroundValue = backgroundValueDefault;
        effectsValue = effectsValueDefault;
        PlayerPrefs.SetInt("currency", currencyDefault);
        PlayerPrefs.SetInt("gameDefaultTime", gameDefaultTimeDefault);
        PlayerPrefs.SetInt("idleTime", idleTimeDefault);
        PlayerPrefs.SetInt("backgroundValue", backgroundValueDefault);
        PlayerPrefs.SetInt("effectsValue", effectsValueDefault);
        PlayerPrefs.Save();
        ComTrans.Instance.audioSource.volume = (float)effectsValue / 100;
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);

        Debug.Log("恢复默认");
    }
    public void DontSave()
    {
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);
        Debug.Log("不保存");
    }

    #endregion

    public void GetData()
    {
        currency = PlayerPrefs.GetInt("currency", currencyDefault);
        gameDefaultTime = PlayerPrefs.GetInt("gameDefaultTime", gameDefaultTimeDefault);
        idleTime = PlayerPrefs.GetInt("idleTime", idleTimeDefault);
        backgroundValue = PlayerPrefs.GetInt("backgroundValue", backgroundValueDefault);
        effectsValue = PlayerPrefs.GetInt("effectsValue", effectsValueDefault);
    }

}
